package com.example.shirotest.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.shirotest.entry.Permission;
import com.example.shirotest.entry.Role;
import com.example.shirotest.entry.User;
import com.example.shirotest.mapper.PermissionMapper;
import com.example.shirotest.mapper.RoleMapper;
import com.example.shirotest.mapper.UserMapper;
import com.example.shirotest.service.UserService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private RoleMapper roleMapper;

    @Autowired
    private PermissionMapper permissionMapper;

    @Override
    public List<User> getUserList() {
        return list();
    }

    @Override
    public IPage<User> selectPageVo(Page<User> page) {
        IPage<User> p = userMapper.selectPageUser(page);

        if(p.getTotal()>0) {
            Map<Integer, Role> allRoles = getAllRoles();
            p.getRecords().forEach( vo -> vo.setRolestr(allRoles.get(vo.getRole()).getName()));
        }
        return p;
    }

    @Override
    public PageInfo<User> selectByPage(Integer page, Integer pagesize, String orderby) {
        PageHelper.startPage(page,pagesize,orderby);
        List<User> pages = userMapper.findPage();
        PageInfo pageInfo = new PageInfo<User>(pages);

        //角色字段提取
        if(pageInfo.getTotal()>0) {
            Map<Integer, Role> allRoles = getAllRoles();
            List<User> list = pageInfo.getList();
            list.forEach(vo -> vo.setRolestr(allRoles.get(vo.getRole()).getName()));
        }

        return pageInfo;
    }

    @Override
    public Boolean updateUserPwd(Integer id, String pwd) {
        User user = userMapper.selectById(id);
        if(null==user)return false;

        user.setPassword(pwd);
        userMapper.updateById(user);

        return true;
    }

    @Override
    public Boolean updateUserLocked(Integer id, Integer locked) {
        User user = userMapper.selectById(id);
        if(null==user)return false;

        user.setLocked(locked);
        userMapper.updateById(user);

        return true;
    }

    @Override
    public Map<Integer, Role> getAllRoles() {
        QueryWrapper<Role> wrapper = new QueryWrapper<>();
        wrapper.gt("id",0);
        List<Role> roleList = roleMapper.selectList(wrapper);

        Map<Integer,Role> roleMap = new HashMap<>();
        roleList.forEach(item->roleMap.put(item.getId(), item));

        return roleMap;
    }

    @Override
    public User getUserByAccount(String account) {
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("account",account);
        return this.getOne(wrapper);
    }

    @Override
    public String getRoleByUid(Integer uid) {
        User user = userMapper.selectById(uid);
        if(null==user)return "";
        Role role = roleMapper.selectById(user.getRole());
        return role.getName();
    }

    @Override
    public Set<String> getPermsByUid(Integer uid) {
        User user = userMapper.selectById(uid);
        if(null==user)return null;

        Set<String> permission = new HashSet<String>();

        QueryWrapper<Permission> wrapper = new QueryWrapper<>();
        wrapper.eq("rid",user.getRole());
        List<Permission> permissions = permissionMapper.selectList(wrapper);
        permissions.forEach(item->permission.add(item.getPermission()));

        return permission;
    }
}
